package com.yz.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yz.common.annotation.Login;
import com.yz.common.annotation.LoginUser;
import com.yz.common.entity.LogEntity;
import com.yz.common.entity.LogForm;
import com.yz.common.entity.ProjectEntity;
import com.yz.common.entity.UserEntity;
import com.yz.common.utils.R;
import com.yz.common.validator.ValidatorUtils;
import com.yz.dao.LogDao;
import com.yz.form.GetListForm;
import com.yz.form.GetSearchListForm;
import com.yz.service.LogService;
import com.yz.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.Min;
import java.util.Date;
import java.util.List;

/**
 *日记相关操作
 *
 * @author yz

 *
 */
@RestController
@RequestMapping("/api")
@Api(tags="日记")
public class ApiLogController {
    @Autowired
    private LogService logService;

    @GetMapping("getLogDetail")
    @ApiOperation("查看日记详情")
    @Login
    public R getLogDetail(@RequestHeader("token")String token,@RequestParam("logId") long logId){
        try {
            LogForm data = logService.getDetails(logId);
            return R.ok().put("data",data);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteLog")
    @ApiOperation("删除日记")
    @Login
    public R deleteLog(@RequestHeader("token")String token,@RequestParam("logId") long logId){
        try {
            logService.deleteMe(logId);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postLog")
    @ApiOperation("提交或修改日记")
    @Login
    public R postLog(@RequestHeader("token")String token, @RequestBody LogForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            logService.postMe(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getLogList")
    @ApiOperation("获取日记列表")
    @Login
    public R getLogList(@RequestHeader("token")String token,@RequestParam("order") int order,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam("type") int type,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        int total=0;
        Page<LogEntity> logs=null;
        String group = "update_at desc,id desc";
        if(order!=0){
            group = "update_at asc,id asc";
        }
        if(keyWord==null||keyWord.isEmpty()){
            total = logService.selectCount(new EntityWrapper<LogEntity>().eq("create_user",user.getUsername()).eq("project_id",projectId).eq("type",type));
            logs = logService.selectPage(new Page<LogEntity>(page,pageSize),new EntityWrapper<LogEntity>().eq("create_user",user.getUsername()).eq("project_id",projectId).eq("type",type).groupBy(group));
        }else {
            total = logService.selectCount(new EntityWrapper<LogEntity>().eq("create_user",user.getUsername()).where("(details like '%"+keyWord+"%' or pit_number like '%"+keyWord+"%')").eq("project_id",projectId).eq("type",type));
           logs = logService.selectPage(new Page<LogEntity>(page,pageSize),new EntityWrapper<LogEntity>().eq("create_user",user.getUsername()).where("(details like '%"+keyWord+"%' or pit_number like '%"+keyWord+"%')").eq("project_id",projectId).eq("type",type).groupBy(group));

        }
        return R.ok().put("data",logs.getRecords()).put("total",total);
    }
}
